const app = getApp()
var http = require('../../../utils/http')
var utils = require('../../../utils/util')
Page({
    data:{
        currentIndex: 0, //默认是活动项
        //分页信息
		queryInfo: {
			//页码
			pageNumber: 1,
			//页数
			pageSize: 5,
			//搜索字段
			queryString: ''
        },
        //表格数据
        tableList: [],
    },

    //点击tab时触发
    titleClick: function (e) {
        this.setData({
            //拿到当前索引并动态改变
            currentIndex: e.currentTarget.dataset.idx,
            'queryInfo.pageNumber':1,
            tableList:[]
        })
        this.findPage(e.currentTarget.dataset.type)
    },
    // 切换swiper-item触发bindchange事件
    pagechange: function (e) {
            console.log(e);
        // 通过touch判断，改变tab的下标值
        if ("touch" === e.detail.source) {
            let currentPageIndex = this.data.currentIndex ;
            //    currentPageIndex = (currentPageIndex + 1) % 3;
            // 拿到当前索引并动态改变
            this.setData({
                currentIndex: currentPageIndex,
            })
            
        }
    },
    findPage(type){
        let exetype = type;
        const that = this;
        this.setData({
            'queryInfo.queryString': exetype+'::favourite'
        })       
        
        http.post("/looklikefavourite/findPageByNewMomentCourse",this.data.queryInfo,res=>{
            let temp = this.data.tableList;
				//将新的结果接收
                let result1 = res.data.rows;
                let result = result1.map((item, index) => {
                    //时间格式化
                    item.createTime = utils.formatTime(item);
                    const content = item.content
                    if (item.title == null) {
                        item.content = utils.toHtml(item.content)
                    }else{
                        item.content = utils.toMarkdown(item.content)
                    }
                    //从文章中提取图片链接
                    return Object.assign(item, {
                        'imageURL': utils.get_url(content)
                    })
                })
                utils.pageHelper(result,temp,this)
            
               console.log(that.data.tableList);
                
        })
        
    },
    //跳转到详情页
	toarticleDetails(event) {
        let item = event.currentTarget.dataset.item
        Object.assign(item,{'topicType':'new'})
		wx.navigateTo({
			url: '/pages/articleDetails/articleDetails?item=' + encodeURIComponent(JSON.stringify(item)),
		})
    },
    
    toDetails(event) {
        const item = event.currentTarget.dataset.item;
        console.log(item);
        wx.navigateTo({
            url: `/pages/course/details/details?item=` + encodeURIComponent(JSON.stringify(item)),
        })
    },


    onLoad:function(options){
       this.findPage("moment")
    },
    onReady:function(){
       
    },
    onShow:function(){
        
    },
    onPullDownRefresh: function() {
        
    },
    onReachBottom: function() {
        
    }
})